package com.xinzhitong.www.mapper;

import com.xinzhitong.www.pojo.TestInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * question_bank相关表的相关操作
 *
 * @author 徐鑫 魏骆淳 钟伟良
 */
@Repository
public interface QuestionBankMapper {

    @Select("SELECT id, `name`, picture, price, provider, professionId, enrollCount " +
            "FROM " +
            "user_has_question_bank_view " +
            "WHERE " +
            "username=#{username} " +
            "AND professionId=#{professionId} UNION ALL " +
            "SELECT id, `name`, picture, price, provider, professionId, enrollCount " +
            "FROM " +
            " question_bank  " +
            "WHERE " +
            " id NOT IN ( " +
            " SELECT " +
            "  id  " +
            " FROM " +
            "  user_has_question_bank_view  " +
            " WHERE " +
            "  username=#{username} ) " +
            " AND professionId=#{professionId}")
    List<Map<String, Object>> selectQuestionBanks(
            @Param("username") String username,
            @Param("professionId") Integer professionId
    );

    @Select("select * from answer_is_correct_view where id = #{questionId} and type = #{questionType}")
    List<Map<String, Object>> selectAnswerIsCorrect(
            @Param("questionId") Integer questionId,
            @Param("questionType") String questionType
    );

    @Insert("insert into user_answer_question(username, question_id, is_correct, users_answer) " +
            "values (#{username}, #{questionId}, #{isCorrect}, #{usersAnswer})")
    void insertUserAnswerQuestion(
            @Param("username") String username,
            @Param("questionId") Integer questionId,
            @Param("isCorrect") boolean isCorrect,
            @Param("usersAnswer") String usersAnswer
    );

    @Select("select optionCode " +
            "from answer_is_correct_view " +
            "where id = #{questionId} and optionIsCorrect = #{optionIsCorrect}")
    List<Map<String, Object>> selectOptionCode(
            @Param("questionId") Integer questionId,
            @Param("optionIsCorrect") Boolean optionIsCorrect
    );

    @Select("select questionId from test_has_question_view where testId=#{testId} and type=#{type}")
    List<Integer> selectQuestionIdOfTestByType(@Param("testId") Integer testId, @Param("type") String type);

    @Select("select * from test_view where credentialId=#{credentialId} and questionCount>0 ORDER BY RAND() LIMIT 1")
    TestInfo selectRandomTestIdOfCredential(@Param("credentialId") Integer credentialId);

    @Select("SELECT type,answerNumber FROM question_information_all where username=#{username}and id=#{id}")
    List<Map<String, Object>> selectQuestionInformation(String username, Integer id);

    @Select("SELECT type,allNumber FROM question_number_end where  id=#{id}")
    List<Map<String, Object>> selectQuestionNumber(Integer id);

    @Select("SELECT code,optionText,analysis FROM question_analysis where id=#{id} AND optionIsCorrect=1")
    List<Map<String, Object>> selectOptionQuestionAnalysis(Integer id);

    @Select("SELECT type FROM question where id=#{id} ")
    String selectType(Integer id);

    @Select("SELECT analysis,isCorrect FROM question_analysis where id=#{id} ")
    List<Map<String, Object>> selectJudgeQuestionAnalysis(Integer id);

    @Select("SELECT analysis,text FROM question_analysis where id=#{id} ")
    List<Map<String, Object>> selectTextQuestionAnalysis(Integer id);

    @Select("SELECT type,number FROM question_err where username=#{username}")
    List<Map<String, Object>> selectQuestionErr(String username);

    @Select("SELECT questionText,questionId FROM question_name where bankId=#{bankId} AND type=#{type}")
    List<Map<String, Object>> selectQuestionText(Integer bankId, String type);

    @Select("SELECT code,optionText,questionId FROM question_get where bankId=#{bankId} AND " +
            "(type='单选' OR type='多选')")
    List<Map<String, Object>> selectQuestionOpinionText(Integer bankId);

    @Select("SELECT questionText FROM question_get where bankId=#{bankId} AND type='判断'")
    List<Map<String, Object>> selectJudgeText(Integer bankId);

    @Select("SELECT questionText FROM question_get where bankId=#{bankId} AND type=#{type}")
    List<Map<String, Object>> selectCommunicateText(Integer bankId, String type);

    @Select("SELECT question_id from user_err_question where username=#{username}")
    List<Integer> selectErr(String username);

    @Select("select * from test_profession_view where professionId=#{professionId}")
    List<Map<String, Object>> selectTestsByProfessionId(Integer professionId);

    @Select("SELECT text FROM question where id=#{id}")
    String selectErrQuestionText(Integer id);

    @Select("SELECT code,optionText FROM question_get where questionId=#{id} ")
    List<Map<String, Object>> selectErrQuestionOpinionText(Integer id);

    @Insert("insert into user_sign_question(username, question_id) values(#{username}, #{questionId})")
    Integer insertUserSignQuestion(String username, Integer questionId);

    @Delete("delete from user_sign_question where username=#{username} and question_id=#{questionId}")
    Integer deleteUserSignQuestion(String username, Integer questionId);

    @Select("select * from user_sign_question where username=#{username} and question_id=#{questionId}")
    Map<String, Object> selectUserSignQuestion(String username, Integer questionId);

    @Select("select * from question where id=#{id}")
    Map<String, Object> selectQuestionById(Integer id);

    @Select("select usersAnswer from user_answer_question_view " +
            "where username = #{username} and questionId = #{questionId}")
    List<Map<String, Object>> selectUserAnswerQuestion(
            @Param("username") String username,
            @Param("questionId") Integer questionId
    );

    @Select("select questionId from question_bank_card_sign where username = #{username} and bankId = #{bankId}" +
            " and type = #{type}")
    Integer[] selectCardSign(String username, Integer bankId, String type);

    @Select("select questionId from question_bank_card_answer where usernameAnswer = #{username} and " +
            "bankId = #{bankId} " +
            " and iscorrect=1 and type=#{type}")
    Integer[] selectCardCorrect(String username, Integer bankId, String type);

    @Select("select questionId from question_bank_card_answer where usernameAnswer = #{username} " +
            "and bankId = #{bankId} " +
            " and iscorrect=0 and type=#{type}")
    Integer[] selectCardErr(String username, Integer bankId, String type);

    @Select("select questionId from question_bank_card_question where  bankId = #{bankId} " +
            " and type=#{type}")
    Integer[] selectCardQuestion(Integer bankId, String type);

    @Select("select * from question_bank where id=#{id}")
    Map<String, Object> selectQuestionBankById(Integer id);

    @Select("select name,price,date,id from question_message where id=#{id} and username=#{username}")
    Map<String, Object> selectQuestionMessage(String username, Integer id);

}
